<?php
class MisionesController extends AppController{
	var $name = 'Misiones';
	var $components = array('Paginator');
	var $helpers = array('Html', 'Form');
	
	function index(){
		//$this->set('misiones', $this->Misione->find('all'));	
		$this->paginate = array(
				'order' => 'Misione.id',
				'conditions' => array('Misione.Deleted' => 0),
				'prefix' => 'Misione',
				'limit' => 10
		);
		
		$this->set('misiones', $this->Paginator->paginate($this->Misione));
	}
	
	function add(){
		
		if(!empty($this->data)){
			if (empty($this->data['Misione']['logo']['name'])){
				$imagen = "imapordefecto.jpg";	
			}
			
			$mision = array();
			$this->Misione->create();
			$this->Misione->id = null;
			$mision['Misione']['nombre'] = $this->data['Misione']['nombre'];
			$mision['Misione']['descripcion'] = $this->data['Misione']['descripcion'];
			$mision['Misione']['logo'] = $this->data['Misione']['logo']['name'];
			
			if($this->Misione->save($mision)){
				$this->Session->setFlash('La mision ha sido ingresada correctamemte.');
				$this->redirect(array('action' => 'index'), null, true);			
			} 
			else{
				$this->Session->setFlash('Mision no ingresada. Pruebe de nuevo.');
			}
		}
	}

	function edit($id = null){
		$this->set('id', $id);
		
		if(!$id){
			$this->Session->setFlas('Mision Invalida');
			$this->redirect(array('action'=>'index'), null, true);
		}
		
		if(empty($this->data)){
			$this->data = $this->Misione->find('all', array('recursive' => 1, 'conditions' => array('Misione.id' => $id)));	
			$this->data = $this->data['0'];
		}
		else{
			$mision = array();
			$this->Misione->id = null;
			$mision['Misione']['id'] = $this->data['Misione']['id'];
			$mision['Misione']['nombre'] = $this->data['Misione']['nombre'];
			$mision['Misione']['descripcion'] = $this->data['Misione']['descripcion'];
			$mision['Misione']['logo'] = $this->data['Misione']['logo']['name'];
			
			if($this->Misione->save($mision)){
				$this->Session->setFlash('La mision ha sido editada');
				$this->redirect(array('action' => 'index'), null, true);
			}
			else{
				$this->Session->setFlash('La mision no ha podido ser editada, intentelo de nuevo');
			}
		}
	}

	function delete($id = null){
		if(!$id){
			$this->Session->setFlash('id invalido para la Mision');
			$this->redirect(array('action' => 'index'), null, true);
		}
		else{
			$mision = array();
			$this->Misione->id = null;
			$mision['Misione']['id'] = $id;
			$mision['Misione']['deleted'] = 1;
			if($this->Misione->save($mision)){
				$this->Session->setFlash('Mision #' . $id . ' borrado');	
				$this->redirect(array('action' => 'index'), null, true);
			}
		}
	}
	
	function reporte(){
		$misiones = $this->Misione->find('all');	
		
		header('Content-type: application/vnd.ms-excel');
			header("Content-Disposition: attachment; filename=\"Reporte_Misiones.xls\"");
			header("Pragma: no-cache");
			header("Expires: 0");
echo <<<html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>eClass</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
html;

			echo '<table border="1">'
			. '<tr>'
			. '<th>Id</th>'
			. '<th>Nombre</th>'
			. '<th>Descripcion</th>'
			. '</tr>'
			;

			if (empty($misiones)) {
			echo '
			<tr>
				<td colspan="3" class="center">' . __('No se encontraron registros.', true) . '</td>
			</tr>';
			}
			else {
				foreach ($misiones as $mision) {
					echo ' 
						<tr>
							<td class="center">' . $mision['Misione']['id'] . '</td>
							<td class="center">' . $mision['Misione']['nombre'] . '</td>
							<td class="center">' . $mision['Misione']['descripcion'] . '</td>
					</tr>';
				}
			}
			echo '</table>';
echo <<<html
</body>
</html>
html;

	}
}
?>